import router from './router'
import store from './store'
import NProgress from 'nprogress' // Progress 进度条
import 'nprogress/nprogress.css'// Progress 进度条样式
// import Cookies from 'js-cookie'
import { Message } from 'element-ui'
//
// const whiteList = ['/login'] // 不重定向白名单
router.beforeEach((to, from, next) => {
  NProgress.start()
  // var token = Cookies.get('access-token')
  var token = store.getters.token
  if (token === undefined || token === 'undefined' || token === '') {
    // if (to.path === '/dashboard') {
    //   next('/login')
    // }
    if (to.path === '/login') {
      next()
    } else {
      next('/login')
    }
  } else {
    store.dispatch('getMenuByUserName').then(response => {
      // store.getters.menuList = response.data.menuList
      if (response.message === 'TIMEOUT') {
        next('/login')
      } else {
        // Cookies.set('menu_list', JSON.stringify(response.data.principal.menuList))
        // console.log(Cookies.get('menu_list'))
        // Cookies.set('resource_list', response.data.principal.resourceList)
        store.commit('SET_MENU_LIST', response.data.principal.menuList)
        store.commit('SET_RESOURCES', response.data.principal.resourceList)
        next()
      }
    }).catch(() => {
      Message({
        message: '无/jwt/me权限,请先授权',
        type: 'warning',
        duration: 3 * 1000
      })
      // Cookies.remove('username')
      store.commit('SET_TOKEN', '')
      next('/login')
    })
  }
})

router.afterEach(() => {
  NProgress.done() // 结束Progress
})
